<script setup>
import { ref, reactive, onMounted, computed } from 'vue';
import { ElMessage, ElMessageBox, ElTable, ElTableColumn, ElButton, ElInput, ElForm, ElFormItem, ElDialog, ElSelect, ElOption, ElPagination } from 'element-plus';

// 线索池数据
const cluePoolData = ref([]);

// 搜索条件
const searchForm = reactive({
  clueId: '',
  phone: '',
  source: ''
});

// 分页相关
const currentPage = ref(1);
const pageSize = ref(10);

// 对话框状态
const dialogVisible = ref(false);
// 当前领取的线索ID
const currentClueId = ref(null);

// 加载线索池数据
const loadCluePoolData = () => {
  // 模拟API请求，使用静态数据
  cluePoolData.value = [
    {
      id: 4,
      clueId: '15',
      phone: '13800138001',
      name: '王小美',
      gender: '女',
      age: 25,
      activityInfo: '线下活动',
      createTime: '2025-10-15 09:23:15',
      updateTime: '2025-10-15 09:23:15'
    },
    {
      id: 5,
      clueId: '22',
      phone: '13700137002',
      name: '赵强',
      gender: '男',
      age: 28,
      activityInfo: '线上活动',
      createTime: '2025-10-14 16:45:30',
      updateTime: '2025-10-14 16:45:30'
    },
    {
      id: 6,
      clueId: '30',
      phone: '13600136003',
      name: '刘芳',
      gender: '女',
      age: 24,
      activityInfo: '推广介绍',
      createTime: '2025-10-13 14:12:45',
      updateTime: '2025-10-13 14:12:45'
    }
  ];
};

// 计算总数
const total = computed(() => cluePoolData.value.length);

// 计算当前页显示的数据
const currentPageData = computed(() => {
  const start = (currentPage.value - 1) * pageSize.value;
  const end = start + Number(pageSize.value);
  return cluePoolData.value.slice(start, end);
});

// 搜索
const handleSearch = () => {
  currentPage.value = 1;
};

// 重置搜索条件
const handleReset = () => {
  Object.keys(searchForm).forEach(key => {
    searchForm[key] = '';
  });
  currentPage.value = 1;
};

// 处理分页
const handleCurrentChange = (val) => {
  currentPage.value = val;
};

// 处理每页条数变化
const handleSizeChange = (val) => {
  pageSize.value = val;
  currentPage.value = 1;
};

// 领取线索
const claimClue = (row) => {
  ElMessageBox.confirm('确定要领取该线索吗？', '领取确认', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'info'
  }).then(() => {
    const index = cluePoolData.value.findIndex(item => item.id === row.id);
    if (index !== -1) {
      cluePoolData.value.splice(index, 1);
      ElMessage.success('领取成功');
    }
  }).catch(() => {
    // 取消领取
  });
};

// 批量领取线索
const batchClaimClue = () => {
  if (cluePoolData.value.length === 0) {
    ElMessage.warning('暂无线索可领取');
    return;
  }
  
  ElMessageBox.confirm('确定要领取全部线索吗？', '批量领取确认', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'info'
  }).then(() => {
    cluePoolData.value = [];
    ElMessage.success('批量领取成功');
  }).catch(() => {
    // 取消批量领取
  });
};

// 组件挂载时加载数据
onMounted(() => {
  loadCluePoolData();
});
</script>

<template>
  <div class="clue-pool-container">
    <!-- 搜索区域 -->
    <div class="search-area">
      <el-form :model="searchForm" label-width="80px" inline>
        <el-form-item label="线索ID">
          <el-input v-model="searchForm.clueId" placeholder="请输入线索ID" style="width: 150px;"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="searchForm.phone" placeholder="请输入手机号" style="width: 150px;"></el-input>
        </el-form-item>
        <el-form-item label="渠道来源">
          <el-select v-model="searchForm.source" placeholder="请选择渠道来源" style="width: 150px;">
            <el-option label="推广介绍" value="promotion"></el-option>
            <el-option label="线上活动" value="online_activity"></el-option>
            <el-option label="线下活动" value="offline_activity"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
          <el-button type="success" @click="batchClaimClue">批量领取</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 表格区域 -->
    <div class="table-area">
      <el-table :data="currentPageData" stripe style="width: 100%">
        <el-table-column prop="clueId" label="线索ID" width="100"></el-table-column>
        <el-table-column prop="phone" label="手机号" width="150"></el-table-column>
        <el-table-column prop="name" label="客户姓名" width="120"></el-table-column>
        <el-table-column prop="gender" label="性别" width="80"></el-table-column>
        <el-table-column prop="age" label="年龄" width="80"></el-table-column>
        <el-table-column prop="activityInfo" label="活动信息" width="150"></el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
        <el-table-column prop="updateTime" label="更新时间" width="180"></el-table-column>
        <el-table-column label="操作" width="100" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" @click="claimClue(row)">领取</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页区域 -->
    <div class="pagination-area">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[5, 10, 20, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      ></el-pagination>
    </div>
  </div>
</template>

<style scoped>
.clue-pool-container {
  padding: 20px;
}

.search-area {
  background-color: #f5f7fa;
  padding: 15px;
  border-radius: 4px;
  margin-bottom: 20px;
}

.table-area {
  background-color: #fff;
  border-radius: 4px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.pagination-area {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.el-button + .el-button {
  margin-left: 10px;
}
</style>